#include <e.h>
#include "e_mod_main.h"
#include "config.h"

/************ private funcs **************************/
static void _e_cb_mouse_down(void *data, int type, void *event_info);
static void _e_cb_mouse_up(void *data, int type, void *event_info);
/*****************************************************/

/* module setup */
EAPI E_Module_Api e_modapi = 
{
   E_MODULE_API_VERSION,
   "SiMBA"
};

static Ecore_Event_Handler *_e_mouse_down_handler = NULL;
static Ecore_Event_Handler *_e_mouse_up_handler = NULL;

EAPI void *
e_modapi_init(E_Module *m)
{
   _e_mouse_down_handler = ecore_event_handler_add(ECORE_EVENT_MOUSE_BUTTON_DOWN, _e_cb_mouse_down, NULL);
   _e_mouse_up_handler = ecore_event_handler_add(ECORE_EVENT_MOUSE_BUTTON_UP, _e_cb_mouse_up, NULL);
   
   return m;
}

EAPI int
e_modapi_shutdown(E_Module *m)
{
    E_FN_DEL(ecore_event_handler_del, _e_mouse_down_handler);
    E_FN_DEL(ecore_event_handler_del, _e_mouse_up_handler);
    
    return 1;
}

EAPI int
e_modapi_save(E_Module *m)
{
   return 1;
}

/************************* Private funcs ****************************************/

static unsigned int timestamp = -1;

static void 
_e_cb_mouse_down(void *data, int type, void *event_info)
{
   Ecore_Event_Mouse_Button   *ev;
   
   ev = event_info;
    if (timestamp == -1)
     {
        timestamp = ev->timestamp;
     }
     ecore_event_evas_mouse_button_down(data, type, ev);
}

static void
_cb_event_mouse_up(void *data, int type, void *event_info)
{
    static unsigned int LIMIT = 3*1000;
    Ecore_Event_Mouse_Button   *ev;
   
   ev = event_info;
   if (timestamp != -1 && (ev->timestamp - timestamp) >= LIMIT)
   { 
      timestamp = -1;
      ev->buttons = 3;
      ecore_event_evas_mouse_button_up(data, type, ev);
   } else {
        ev->buttons = 1;
        ecore_event_evas_mouse_button_up(data, type, ev);
   }
}
/***********************************************************************/
